package com.liaozl.sca.account.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.liaozl.sca.account.dto.AccountDTO;
import com.liaozl.sca.account.feign.AccountFeign;
import com.liaozl.sca.account.sentinel.SentinelBlockHandler;
import com.liaozl.sca.account.sentinel.SentinelFallback;
import com.liaozl.sca.common.dto.Result;
import com.liaozl.sca.common.enums.ResultCodeEnum;
import com.liaozl.sca.common.exception.AccountException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Description:
 * @version: v1.0.0
 * @author: liaozuliang
 * @date: 2021/9/17 17:42
 */
@Slf4j
@RestController
public class AccountController implements AccountFeign {

    @Override
    public Result<Boolean> insert(@RequestBody AccountDTO accountDTO) {
        log.info("=====insert=====");
        return Result.success(true);
    }

    @Override
    public Result<Boolean> delete(@RequestParam("userId") Long userId) {
        log.info("=====delete=====");
        return Result.success(true);
    }

    @Override
    public Result<Boolean> delete2(@RequestParam("userId") Long userId) {
        log.info("=====delete2=====");
        if (true) {
            throw new AccountException(ResultCodeEnum.SYSTEM_ERROR);
        }
        return Result.success(true);
    }

    @Override
    public Result<Boolean> update(@RequestBody AccountDTO accountDTO) {
        log.info("=====update=====");
        return Result.success(true);
    }

    @SentinelResource(value = "getByUserId",
            blockHandlerClass = SentinelBlockHandler.class,
            blockHandler = "getByUserIdHandler"
    )
    @Override
    public Result<AccountDTO> getByUserId(@PathVariable("userId") Long userId) {
        log.info("=====getByUserId=====");
        AccountDTO accountDTO = new AccountDTO();
        accountDTO.setUserId(userId);
        return Result.success(accountDTO);
    }

    @SentinelResource(value = "getByUserId2",
            fallbackClass = SentinelFallback.class,
            fallback = "getByUserId2Fallback"
    )
    @Override
    public Result<AccountDTO> getByUserId2(@PathVariable("userId") Long userId) {
        log.info("=====getByUserId2=====");

        if (true) {
            throw new AccountException(ResultCodeEnum.SYSTEM_ERROR);
        }

        AccountDTO accountDTO = new AccountDTO();
        accountDTO.setUserId(userId);
        return Result.success(accountDTO);
    }
}
